/**
 * ReverseOrderCursor.java
 * Copyright (C) 2011 Yuanyan<yuanyan.cao@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * For information on how to redistribute this software under
 * the terms of a license other than GNU General Public License
 * contact yuanyan.cao@gmail.com
 */
package edu.rabbit.kernel.table;

import java.io.InputStream;
import java.util.Map;


import edu.rabbit.DbException;
import edu.rabbit.ValueType;
import edu.rabbit.schema.ConflictAction;
import edu.rabbit.table.ICursor;

/**
 * @author Yuanyan<yanyan.cao@gmail.com>
 * 
 * 
 */
public class ReverseOrderCursor implements ICursor {

    private ICursor iCursor;
    private boolean eof;

    public ReverseOrderCursor(ICursor iCursor) throws DbException {
        this.iCursor = iCursor;
        first();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#close()
     */
    public void close() throws DbException {
        iCursor.close();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#delete()
     */
    public void delete() throws DbException {
        if (!eof) {
            iCursor.delete();
            eof = iCursor.eof();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#eof()
     */
    public boolean eof() throws DbException {
        return eof;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#first()
     */
    public boolean first() throws DbException {
        return !(eof = !iCursor.last());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#getBlobAsArray(int)
     */
    public byte[] getBlobAsArray(int field) throws DbException {
        return iCursor.getBlobAsArray(field);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#getBlobAsArray(java.lang
     * .String)
     */
    public byte[] getBlobAsArray(String fieldName) throws DbException {
        return iCursor.getBlobAsArray(fieldName);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#getBlobAsStream(int)
     */
    public InputStream getBlobAsStream(int field) throws DbException {
        return iCursor.getBlobAsStream(field);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#getBlobAsStream(java.lang
     * .String)
     */
    public InputStream getBlobAsStream(String fieldName) throws DbException {
        return iCursor.getBlobAsStream(fieldName);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#getBoolean(int)
     */
    public boolean getBoolean(int field) throws DbException {
        return iCursor.getBoolean(field);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#getBoolean(java.lang.String
     * )
     */
    public boolean getBoolean(String fieldName) throws DbException {
        return iCursor.getBoolean(fieldName);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#getFieldType(int)
     */
    public ValueType getFieldType(int field) throws DbException {
        return iCursor.getFieldType(field);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#getFieldType(java.lang.
     * String)
     */
    public ValueType getFieldType(String fieldName) throws DbException {
        return iCursor.getFieldType(fieldName);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#getFieldsCount()
     */
    public int getFieldsCount() throws DbException {
        return iCursor.getFieldsCount();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#getFloat(int)
     */
    public double getFloat(int field) throws DbException {
        return iCursor.getFloat(field);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#getFloat(java.lang.String)
     */
    public double getFloat(String fieldName) throws DbException {
        return iCursor.getFloat(fieldName);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#getInteger(int)
     */
    public long getInteger(int field) throws DbException {
        return iCursor.getInteger(field);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#getInteger(java.lang.String
     * )
     */
    public long getInteger(String fieldName) throws DbException {
        return iCursor.getInteger(fieldName);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#getRowId()
     */
    public long getRowId() throws DbException {
        return iCursor.getRowId();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#getString(int)
     */
    public String getString(int field) throws DbException {
        return iCursor.getString(field);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#getString(java.lang.String)
     */
    public String getString(String fieldName) throws DbException {
        return iCursor.getString(fieldName);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#getValue(int)
     */
    public Object getValue(int field) throws DbException {
        return iCursor.getValue(field);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#getValue(java.lang.String)
     */
    public Object getValue(String fieldName) throws DbException {
        return iCursor.getValue(fieldName);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#goTo(long)
     */
    public boolean goTo(long rowId) throws DbException {
        return iCursor.goTo(rowId);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#isNull(int)
     */
    public boolean isNull(int field) throws DbException {
        return iCursor.isNull(field);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#isNull(java.lang.String)
     */
    public boolean isNull(String fieldName) throws DbException {
        return iCursor.isNull(fieldName);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#last()
     */
    public boolean last() throws DbException {
        return !(eof = !iCursor.first());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#next()
     */
    public boolean next() throws DbException {
        return !(eof = !iCursor.previous());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#previous()
     */
    public boolean previous() throws DbException {
        return !(eof = !iCursor.next());
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#update(java.lang.Object[])
     */
    public void update(Object... values) throws DbException {
        iCursor.update(values);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#update(org.tmatesoft.rabbit
     * .core.schema.ConflictAction, java.lang.Object[])
     */
    public void updateOr(ConflictAction onConflict, Object... values) throws DbException {
        iCursor.updateOr(onConflict, values);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#updateByFieldNames(java
     * .util.Map)
     */
    public void updateByFieldNames(Map<String, Object> values) throws DbException {
        iCursor.updateByFieldNames(values);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#updateByFieldNames(org.
     * tmatesoft.rabbit.core.schema.ConflictAction, java.util.Map)
     */
    public void updateByFieldNamesOr(ConflictAction onConflict, Map<String, Object> values) throws DbException {
        iCursor.updateByFieldNamesOr(onConflict, values);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#updateWithRowId(long,
     * java.lang.Object[])
     */
    public long updateWithRowId(long rowId, Object... values) throws DbException {
        return iCursor.updateWithRowId(rowId, values);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.tmatesoft.rabbit.core.table.ICursor#updateWithRowId(org.tmatesoft
     * .rabbit.core.schema.ConflictAction, long, java.lang.Object[])
     */
    public long updateWithRowIdOr(ConflictAction onConflict, long rowId, Object... values) throws DbException {
        return iCursor.updateWithRowIdOr(onConflict, rowId, values);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#reverse()
     */

    public ICursor reverse() throws DbException {
        return new ReverseOrderCursor(this);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#getCurrentRow()
     */
    public long getRowIndex() throws DbException {
        return iCursor.getRowIndex();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#getRowCount()
     */
    public long getRowCount() throws DbException {
        return iCursor.getRowCount();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#goToRow(long)
     */
    public boolean goToRow(long rowIndex) throws DbException {
        return iCursor.goToRow(rowIndex);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#getLimit()
     */
    public long getLimit() {
        return iCursor.getLimit();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#setLimit(int)
     */
    public void setLimit(long limit) throws DbException {
        iCursor.setLimit(limit);
    }
}
